package com.systop.service.impl;

import com.systop.domain.to.SucLoginUserTo;
import com.systop.domain.vo.CartChangeVo;
import com.systop.domain.vo.CartItem;
import com.systop.domain.vo.CartVo;
import com.systop.entity.Goods;
import com.systop.entity.SysUser;
import com.systop.interceptor.LoginInterceptor;
import com.systop.service.GoodsService;
import com.systop.service.ShopCartService;
import com.systop.service.SysUserService;
import com.systop.utils.RedisUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description: TODO
 * @Author: jsj
 * @Date: 2023/3/11
 */
@Service
public class ShopCartServiceImpl implements ShopCartService {

    @Autowired
    RedisUtil redisUtil;

    @Autowired
    SysUserService sysUserService;

    @Autowired
    GoodsService goodsService;

    @Autowired
    RedisTemplate redisTemplate;

    public static final String CART_PREFIX = "shopcar:";

    @Override
    public CartItem addToCart(Long goodsId, Integer goodsNum) {
        //获取当前登录用户信息
        SucLoginUserTo sucLoginUserTo = LoginInterceptor.threadLocal.get();
        SysUser user = sysUserService.findUserByUserCode(sucLoginUserTo.getUserCode());
        //设置redisKey
        String cartKey = CART_PREFIX+user.getUserId();

        //查询是否已经存在该商品.
        CartItem getItem = (CartItem) redisUtil.hget(cartKey, goodsId.toString());
        CartItem cartItem = new CartItem();
        if (getItem == null) {
            //获取商品详细信息
            Goods goods = goodsService.getGoodsById(goodsId);

            //填充购物车信息
            BeanUtils.copyProperties(goods, cartItem);
            cartItem.setCount(goodsNum);
            cartItem.setPrice(new BigDecimal(goods.getPrice()));

            //设置相关数据
            redisUtil.hset(cartKey, goodsId.toString(), cartItem);
            return cartItem;
        }else{
            //修改已经存在的商品的数据
            getItem.setCount(getItem.getCount()+goodsNum);
            //设置相关数据
            redisUtil.hset(cartKey, goodsId.toString(), getItem);
            return getItem;
        }
    }

    @Override
    public CartVo getCartList() {
        //获取当前登录用户信息
        SucLoginUserTo sucLoginUserTo = LoginInterceptor.threadLocal.get();
        SysUser user = sysUserService.findUserByUserCode(sucLoginUserTo.getUserCode());
        //设置redisKey
        String cartKey = CART_PREFIX+user.getUserId();

        CartVo cartVo = new CartVo();

        //获取所有商品
        List<CartItem> cartList =
                redisUtil.hmget(cartKey).values().
                        stream().map(item->(CartItem)item).sorted().collect(Collectors.toList());
        // System.out.println(cartList);

        //填充数据
        cartVo.setCartItem(cartList);
        return cartVo;
    }

    @Override
    public void itemCheck(List<CartChangeVo> cartList) {
        //获取当前登录用户信息
        SucLoginUserTo sucLoginUserTo = LoginInterceptor.threadLocal.get();
        SysUser user = sysUserService.findUserByUserCode(sucLoginUserTo.getUserCode());
        //设置redisKey
        String cartKey = CART_PREFIX+user.getUserId();

        //修改状态
        for (CartChangeVo cartChangeVo : cartList) {
            CartItem getItem = (CartItem) redisUtil.hget(cartKey, cartChangeVo.getGoodsId().toString());

            //修改购物车商品数
            if(cartChangeVo.getGoodsNum()!=null && cartChangeVo.getGoodsNum()>0){
                // System.out.println("修改id=>"+cartChangeVo.getGoodsId()+"商品数量");
                getItem.setCount(cartChangeVo.getGoodsNum());
            }
            //修改状态
            else if(cartChangeVo.getIsCheck()!=null){
                // System.out.println("修改id=>"+cartChangeVo.getGoodsId()+"选中状态");
                getItem.setIsCheck(cartChangeVo.getIsCheck());
            }
            redisUtil.hset(cartKey, cartChangeVo.getGoodsId().toString(), getItem);
        }
    }

    @Override
    public void delForCart(Long[] goodsIds) {
        //获取当前登录用户信息
        SucLoginUserTo sucLoginUserTo = LoginInterceptor.threadLocal.get();
        SysUser user = sysUserService.findUserByUserCode(sucLoginUserTo.getUserCode());
        //设置redisKey
        String cartKey = CART_PREFIX+user.getUserId();

        for (Long goodsId : goodsIds) {
            redisUtil.hdel(cartKey, goodsId.toString());
        }
    }
}
